package com.digitalart.site.menu {
	import com.greensock.easing.Back;
	import com.greensock.easing.Cubic;
	import com.greensock.TweenMax;

	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;

	/**
	 * @author johan.chaves
	 */
	public class MenuView extends Sprite {

		public static const ON_MENU_VIEW_READY : String = "onmenuviewready";
		public static const HORIZONTAL_TYPE : String = "horizontaltype";
		public static const VERTICAL_TYPE : String = "verticaltype";
		public static const HORIZONTAL_ITEM_SPACE : Number = 10;
		public static const VERTICAL_ITEM_SPACE : Number = 0;
		public static const INITIAL_VERTICAL_Y_POS : Number = 33;
		public static const VERTICAL_X_POS_IN : Number = 10;
		public static const VERTICAL_Y_ANIMATION : Number = 398;		 

		private var _items : Array;

		private var _type : String;

		private var _background : Sprite;
		
		private var _isHide : Boolean;

		public function MenuView() {
			items = new Array();
			_isHide = true;	
			addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);		
		}

		private function onAddedToStage(event : Event) : void {
			showEffect();
		}

		public function init() : void {	
			
			if(isHorizontal()){
				addHorizontalBackground();
				locateHorizontalItems();
				hideHorizontalItems();
			}else{
				addVerticalBackground();
				locateVerticalItems();
			}
			dispatchEvent(new Event(ON_MENU_VIEW_READY));
		}

		private function locateVerticalItems() : void {
			
			var lastY : Number = INITIAL_VERTICAL_Y_POS;
			for each(var item : MenuItem in items) {
				item.x = -item.width;
				item.y = lastY + MenuView.VERTICAL_ITEM_SPACE;
				lastY = item.y + item.height;
				addChild(item);
			}
		}

		private function locateHorizontalItems() : void {
			
			var lastX : Number = this.width;
			for each(var item : MenuItem in items) {
				item.x = lastX - item.width - MenuView.HORIZONTAL_ITEM_SPACE;
				lastX = item.x;
				addChild(item);
			}
		}
		
		public function showEffect():void{
			
			if(isHorizontal()){
				showHorizontalEffect();
			}else{
				showVerticalEffect();				
			}
		}
		
		private function showVerticalEffect() : void {
			
			if(_isHide == true) {
					(_background as menuBg_FC).upArrow.visible = false;
					(_background as menuBg_FC).downArrow.visible = true;						
						TweenMax.to(this,0.5,{x:0 , y:VERTICAL_Y_ANIMATION,ease:Cubic.easeOut,onStart:showItemsVerticalEffect,onStartParams:[0]});
						_isHide = false;	
				}else{	
					(_background as menuBg_FC).upArrow.visible = true;
					(_background as menuBg_FC).downArrow.visible = false;	
					TweenMax.to(this,0.8,{x:0 , y:MenuController.VERTICAL_MENU_Y_POS, ease:Cubic.easeOut, onComplete : hideVerticalItems});										
					_isHide = true;
				}
		}
		
		private function showHorizontalEffect() : void {
			
			if(_isHide == true) {	
					(_background as topMenuBg_FC).upArrow.visible = true;
					(_background as topMenuBg_FC).downArrow.visible = false;												
					TweenMax.to(this,0.5,{x:0 , y:0,ease:Cubic.easeOut,onStart:showItemsHorizontalEffect,onStartParams:[0,true]});
					_isHide = false;	
				}else{
					(_background as topMenuBg_FC).upArrow.visible = false;
					(_background as topMenuBg_FC).downArrow.visible = true;
					TweenMax.to(this,0.5,{x:0 , y:MenuController.HORIZONTAL_MENU_Y_POS, ease:Cubic.easeOut,onComplete : hideHorizontalItems});
					_isHide = true;
				}
		}
		
		private function showItemsVerticalEffect(id : int) : void {
			if(this.items[id] != null){
				TweenMax.to(this.items[id],0.8,{x:10, y: this.items[id].y  , ease:Cubic.easeOut,onStart:showItemsVerticalEffect,onStartParams:[id + 1]});			
			}				
		}
		
		private function hideHorizontalItems():void{
			
			for each(var item:MenuItem in items){
				item.y = -item.height;
			}
		}
		
		private function hideVerticalItems():void{
			
			for each(var item:MenuItem in items){
				item.x = - item.width;
			}
		}
		
		private function showItemsHorizontalEffect(id : int,isFirst : Boolean) : void {
			
			if(items[id] != null){
				if(isFirst){					
					TweenMax.to(this.items[id],0.5,{x:this.items[id].x, y: 0, ease:Cubic.easeOut,onStart:showItemsHorizontalEffect,onStartParams:[id + 1,false]});
				}else{					
					TweenMax.to(this.items[id],0.5,{x:this.items[id].x, y: 0,delay:0.1, ease:Cubic.easeOut,onStart:showItemsHorizontalEffect,onStartParams:[id + 1,false]});				
				}
			}
			
		}
		
		public function setItemsUnselected() : void{
			for each(var item : MenuItem in items){
				item.setTextColorUnSelected();
				item.isSelected = false;
			}
		}
		
		public function isHorizontal() : Boolean {
			var isHorizontal : Boolean;
			switch (type) {
				case HORIZONTAL_TYPE:
					isHorizontal =  true;
					break;
				 
				case VERTICAL_TYPE:
					isHorizontal = false;
					break;
			}
			return isHorizontal;
		}
		
		public function addHorizontalBackground() : void {
			_background = new topMenuBg_FC();
			addChild(_background);
		}

		public function addVerticalBackground() : void {
			_background = new menuBg_FC();
			addChild(_background);
		}

		public function getHeader() : MovieClip {
			
			var header : MovieClip;
			if(isHorizontal()){
				header  = (_background as topMenuBg_FC).headerText;							
			}else{
				header = (_background as menuBg_FC).headerTxt;				
			}
			return header;
		}
		
		public function get items() : Array {
			return _items;
		}

		public function set items(items : Array) : void {
			_items = items;
		}

		public function get type() : String {
			return _type;
		}

		public function set type(type : String) : void {
			_type = type;
		}
	}
}
